/**
 * Created by lance on 2017/2/24.
 */


import { Injectable } from '@angular/core';
import 'rxjs/add/operator/map';
import { Jsonp, URLSearchParams,Response } from '@angular/http';
import { Observable }   from 'rxjs/observable';
import 'rxjs/add/operator/catch';



@Injectable()
export class MainService {
  constructor(private jsonp: Jsonp) {}
   public statu='';
  private showfrom:boolean;

  login (email:string, pwd:string,invitation:string) {
    let wikiUrl = 'http://api.pinwu.net/site/login';
    let params = new URLSearchParams();
    params.set('pwd', pwd);                 // the user's repwd
    params.set('email', email);             // the user's email
    params.set('invitation', invitation);     // the user's name
    params.set('callback', 'JSONP_CALLBACK');

    return this.jsonp
      .get(wikiUrl, { search: params })
      .map((res:Response)=>{
        let result=res.json();
        console.log(result);
        return result;
      });
  }
  category(){
    let wikchek = 'http://api.pinwu.net/site/category';
    let params = new URLSearchParams();
    params.set('callback', 'JSONP_CALLBACK');
    return this.jsonp
      .get(wikchek, { search: params })
      .map((res:Response)=>{
        let result=res.json();
        return result;
      });
  }
  loginchek () {
    let wikchek = 'http://api.pinwu.net/site/login';
    let params = new URLSearchParams();
    params.set('callback', 'JSONP_CALLBACK');
    return this.jsonp
      .get(wikchek, { search: params })
      .map((res:Response)=>{
        let result=res.json();
        console.log(result);
        return result;
      });
  }
  //
  // private handleError (error: Response | any) {
  //   // In a real world app, we might use a remote logging infrastructure
  //   let errMsg: string;
  //   if (error instanceof Response) {
  //     const body = error.json() || '';
  //     const err = body.error || JSON.stringify(body);
  //     errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
  //   } else {
  //     errMsg = error.message ? error.message : error.toString();
  //   }
  //   console.error(errMsg);
  //   return Observable.throw(errMsg);
  // }
}
